﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using nwrole.lib;

namespace nwrole.lib2 {
    public class PluginServiceHost {
        private Guid serviceId;
        private int Port;
        private int SupervisorPort;
        private PluginManager manager;
        private Uri baseAddress;
        private ServiceHost serviceHost;
        private string serviceName;

        private static void WriteLog(string content) {
            DateTime now = DateTime.Now;
            var path = "c:/log/nwrole/PluginService/";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);

            System.IO.File.AppendAllText(path + "log " + now.ToString("yyyy-MM-dd") + ".txt", now.ToString("yyyy-MM-dd hh:mm:ss ") + content + "\r\n");
        }

        public PluginServiceHost(Guid serviceId, string serviceName, int port, int supervisorPort) {
            WriteLog(String.Format("PluginService() '{0}' ({1}).", serviceId, port));
            this.serviceName = serviceName;
            this.serviceId = serviceId;
            Port = port;
            SupervisorPort = supervisorPort;
        }

        public void Start() {
            WriteLog("PluginServiceHost.Start" + String.Format("Starting Plugin service '{0}' ({1}).", serviceId, Port));
            EventLogger.LogInformation("PluginServiceHost.Start", String.Format("Starting Plugin service '{0}' ({1}).", serviceId, Port));

            try {
                if (serviceHost != null)
                    serviceHost.Close();
                if (manager != null)
                    manager.Stop();

                manager = new PluginManager(serviceId, serviceName, SupervisorPort);
                baseAddress = new Uri("net.tcp://localhost:" + Port.ToString());
                serviceHost = new ServiceHost(manager, baseAddress);
                NetTcpBinding binding = new NetTcpBinding();
                binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                binding.MaxReceivedMessageSize = 2147483647;
                serviceHost.AddServiceEndpoint(typeof(IServiceContract), binding, serviceId.ToString("N"));
                serviceHost.Open();

                manager.Start();
            } catch (Exception exception) {
                WriteLog("PluginServiceHost exception: " + exception.ToString());
                EventLogger.LogException("PluginServiceHost.Start", exception);
            }
            EventLogger.LogInformation("PluginServiceHost.Start", String.Format("Started Plugin service '{0}' ({1}).", serviceId, Port));
        }

        public void Stop() {
            WriteLog("PluginServiceHost.Stop" + String.Format("Stopping Plugin service '{0}' ({1}).", serviceId, Port));
            EventLogger.LogInformation("PluginServiceHost.Stop", String.Format("Stopping Plugin service '{0}' ({1}).", serviceId, Port));

            if (serviceHost != null) {
                try {
                    manager.Stop();
                    serviceHost.Close();
                    manager = null;
                    serviceHost = null;
                } catch (Exception exception) {
                    EventLogger.LogException("PluginServiceHost.Stop", exception);
                }
                EventLogger.LogInformation("PluginServiceHost.Stop", String.Format("Stopped Plugin service '{0}' ({1}).", serviceId, Port));
            }
        }
    }
}
